/**
 * v-copyText 复制文本内容
 * Copyright (c) 2022 bige
 */

export default {
  beforeMount(el, binding) {
    if (binding.arg === "callback") {
      el.$copyCallback = binding.value;
    } else {
      el.$copyValue = binding.value;
      initHandler(el, binding)
    }
  },
  updated(el, binding) {
    if (binding.arg === "callback") {
      el.$copyCallback = binding.value;
    } else {
      el.$copyValue = binding.value;
      initHandler(el, binding)
    }
  }
}

/** 绑定操作 */
function initHandler(el, binding) {
  const handler = () => {
    copyTextToClipboard(binding.value);
    if (el.$copyCallback) {
      el.$copyCallback(el.$copyValue);
    }
  }
  el.onclick = handler;
}